//
//  ScenesAudioCodec.h
//  ScenesPlayer
//
//  Created by Branch on 2016/11/2.
//  Copyright © 2016年 Scenes. All rights reserved.
//

#ifndef ScenesAudioCodec_h
#define ScenesAudioCodec_h
#include <stdint.h>
#include <queue>
#include "MutexLock.h"



typedef void(*ScenesAudioCodecCallback)(uint8_t* data,uint32_t len,int64_t presentationTimeUs,
                                        int streamIndex,void* userData);


struct StAudioBuff
{
    uint8_t* buff;
    uint32_t len;
    int64_t presentationTimeUs;
};

typedef struct StAudioBuff  StAudioBuff;

class IAudioDecoderFactory;
class MutexLock;
class IAudioDecoder;

class ScenesAudioCodec {
public:
    
    ScenesAudioCodec();
    
    ~ScenesAudioCodec();
    
    

    
    bool openDecoder(IAudioDecoderFactory* audioDecoderFactory,
                     int streamIndex,
                     ScenesAudioCodecCallback callback,
                     void* userData);
    
    void shutdown();
    
    void clean();
    
    bool inputBuff(uint8_t* buff,uint32_t len,int64_t presentationTimeUs);
    
    void runThread();
    
    uint32_t getCacheSize();
private:
    
    void doDecode();
    void doClean();
private:
    IAudioDecoder* _audioDecoder;
    IAudioDecoderFactory* _audioDecoderFactory;
    int _streamIndex;
    ScenesAudioCodecCallback _scenesAudioCodecCallback;
    void* _userData;
    
    MutexLock _lock;
    MutexLock _queue_lock;
    std::queue<StAudioBuff> _audioQueue;
    
    bool* _isEndThread;
    bool _isClean;
};

#endif /* ScenesAudioCodec_h */
